<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <template v-slot:after>
          <el-button type="primary" size="small" @click="addPerm(1,'0')">添加权限</el-button>
        </template>
      </page-tools>
      <!-- 表格 -->
      <el-table :data="list" border="" row-key="id">
        <el-table-column label="名称" prop="name" align="center" />
        <el-table-column label="标识" prop="code" align="center" />
        <el-table-column label="描述" prop="description" align="center" />
        <el-table-column label="操作" align="center">
          <template v-slot="{row}">
            <el-button v-if="row.type === 1" type="text" @click="addPerm(2,row.id)">添加</el-button>
            <el-button type="text" @click="editRow(row.id)">编辑</el-button>
            <el-button type="text" @click="delRow(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <addForm ref="addForm" />
  </div>
</template>

<script>
import { getPermissionList, getPermissionDetail, delPermissionList } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
import addForm from './components/addForm.vue'
export default {
  name: 'Permission',
  components: {
    addForm
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 获取权限列表
    async getPermissionList() {
      const res = await getPermissionList()
      this.list = tranListToTreeData(res, '0')
    },
    // 添加权限
    addPerm(type, pid) {
      this.$refs.addForm.formData.type = type
      this.$refs.addForm.formData.pid = pid
      this.$refs.addForm.dialogVisible = true
    },
    // 编辑权限(回显)
    async editRow(id) {
      const res = await getPermissionDetail(id)
      this.$refs.addForm.formData = res
      this.$refs.addForm.dialogVisible = true
    },
    // 删除权限
    async delRow(id) {
      await this.$confirm('确认删除吗?')
      await delPermissionList(id)
      this.$message.success('删除成功')
      this.getPermissionList()
    }
  }
}
</script>

<style>

</style>
